<template>
	<view class="setUp">
		
		<view v-if="!isDisableDelAccount" @click="delaccount">
			<view class="btnDelete">注销账户</view>
		</view>
		<view style="height: 50px;"></view>
		<pay-keyboard :show_key="show_pay_keyboard" @hideFun="hidePayKeyboard"
			@getPassword="getPayPassword"></pay-keyboard>
	</view>
</template>

<script>
	import {
		getPersonalCenter,
		changeHeader,
		writeOff,
		reqLoginStatus,
		getAppConfigSync
	} from "../../../api/index.js"
	export default {
		data() {
			return {
				isShowVipExpirationDate: false,
				isShowFenhong: false,
				show_pay_keyboard: false,
				isDisableDelAccount: false,
				date: '暂无',
				isAgency: getApp().globalData.isAgency,
				centerMsg: {},
				userInfo: null
			};
		},
		onShow() {
			this.onGetPersonalCenter()
		},
		onLoad() {
			const appConfig = getAppConfigSync()
			this.isDisableDelAccount = appConfig.isDisableDelAccount
			this.isShowVipExpirationDate = appConfig.isShowVipExpirationDate
			this.isShowFenhong = appConfig.isShowFenhong
		},
		computed: {
			startDate() {
				return this.getDate('start');
			},
			endDate() {
				return this.getDate('end');
			}
		},
		methods: {
			async logout() {
				const res = await reqLoginStatus('s=/ApiIndex/logindata')
				const {
					status,
					msg
				} = res.data
				if (status != 1) {
					this.showToast(msg)
				}
				
				uni.setStorageSync('sessionData', {
					account: '',
					session_id: '',
					socket_token: ''
				})
				
				uni.removeStorageSync('APPCONFIG');

				// #ifdef H5
				this.onSkip('/pages/login/loginAndRegister')
				// #endif

				// #ifdef APP-PLUS
				//更新完重启app
				plus.runtime.restart();
				// #endif
			},
			showPayKeyboard() {
				this.show_pay_keyboard = true
			},
			hidePayKeyboard() {
				this.show_pay_keyboard = false
			},
			getPayPassword(n) {
				console.log("用户输入的密码", n)
			},
			// 改变出生日期
			bindDateChange(e) {
				// 还没对接口
				this.date = e.detail.value
				this.uploadHeadimg({
					birthday: this.date
				})
			},
			// 获取当前时间
			getDate(type) {
				const date = new Date();
				let year = date.getFullYear();
				let month = date.getMonth() + 1;
				let day = date.getDate();
				if (type === 'start') {
					year = year - 60;
				} else if (type === 'end') {
					year = year + 2;
				}
				month = month > 9 ? month : '0' + month;
				day = day > 9 ? day : '0' + day;
				return `${year}-${month}-${day}`;
			},
			async onwriteOff() {
				const res = await writeOff('s=ApiMy/delaccount')
				const {
					status,
					msg
				} = res.data
				this.showToast(msg)
				if (status == 1) {
					setTimeout(() => {
						uni.reLaunch({
							url: '/pages/login/loginAndRegister'
						});
					}, 2000)
				}
			},
			// 注销账号
			async delaccount() {
				const result = await new Promise(resolve => {
					uni.showModal({
						title: '提示',
						content: '注销账号后该账号下的所有数据都将删除并且无法恢复，确定要注销吗？',
						confirmColor: '#41644A',
						success(msg) {
							resolve(msg)
						}
					});
				})
				if (result.confirm) {
					this.onwriteOff()
				}
			},

			//上传图片
			async chooseFile() {
				uni.chooseImage({
					count: 1,
					sourceType: ['album'],
					success: (res) => {
						uni.showLoading({
							title: '上传中...'
						})
						this.upImg(res.tempFilePaths[0])
					}
				});
			},
			upImg(path) {
				let BASE_URL = getApp().globalData.BASE_URL
				let platform = 'app'
				// #ifdef H5
				if (this.isAgency) {
					platform = 'h5'
					BASE_URL = '/api'
				}
				// #endif
				uni.uploadFile({
					url: `${BASE_URL}?s=/ApiImageupload/uploadImg&aid=1&platform=${platform} `,
					filePath: path,
					// #ifndef H5
					header: {
						'content-type': 'multipart/form-data'
					},
					// #endif
					name: 'file',
					success: (res) => {
						const data = JSON.parse(res.data);
						const {
							msg,
							status,
							url
						} = data
						if (status == 1) {
							this.centerMsg.headimg = url
							let a = {
								headimg: url
							}
							this.uploadHeadimg(a)
						}
						uni.hideLoading();
					}
				});
			},
			// 修改信息
			async uploadHeadimg(obj) {
				const res = await changeHeader('s=ApiMy/setfield', obj)
				const {
					status,
					msg
				} = res.data
				this.showToast(msg)
			},

			async onGetPersonalCenter() {
				const res = await getPersonalCenter('s=/ApiZwyRequest/userinfo')
				const {
					data,
					status,
					msg
				} = res.data
				if (status == 1) {
					this.centerMsg = data
					this.date = data.birthday ? data.birthday : '暂无'
				} else {
					this.showToast(msg)
				}
			},
		},
	}
</script>
<style>
	page {
		background-color: #f7f7f7;
	}
</style>
<style lang="scss">
	.setUp {
		padding-top: 10upx;

		.list {
			margin-bottom: 30upx;

			>view {
				display: flex;
				justify-content: space-between;
				align-items: center;
				padding: 40upx 30upx;
				box-sizing: border-box;
				background-color: #fff;
				border-bottom: 1px solid #f7f7f7;

				.tips {
					font-size: 32upx;
				}

				.texts {
					font-size: 32upx;
					color: #666666;
					display: flex;
					align-items: center;

					.text_val {
						margin-right: 20upx;
					}
				}
			}

			.head_img {
				padding: 20upx 30upx;
				box-sizing: border-box;

				image {
					width: 80upx;
					height: 80upx;
					border-radius: 50%;
					margin-right: 20upx;
				}
			}

		}

		.btn {
			margin-top: 20rpx;
			width: 100%;
			height: 96upx;
			line-height: 96upx;
			text-align: center;
			color: #41644A;
			font-size: 32upx;
			font-weight: bold;
			background-color: #fff;
		}

		.btnDelete {
			margin-top: 20rpx;
			width: 100%;
			height: 96upx;
			line-height: 96upx;
			text-align: center;
			font-weight: bold;
			color: red;
			font-size: 18px;
			background-color: #fff;
		}
	}
</style>